﻿Imports lbGlobalcom
Public Class fMarcas
    Dim daMarcas As dsProductosTableAdapters.PRO_MARCASTableAdapter
    Dim daClave As dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
    Private Sub btnCargarMarcas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarMarcas.Click
        Try
            If Me.DsProductos.PRO_MARCAS.GetChanges() IsNot Nothing Then
                PRO_MARCASBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    PRO_MARCASBindingNavigatorSaveItem.PerformClick()
                End If
            End If
            daMarcas.Fill(DsProductos.PRO_MARCAS)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub PRO_MARCASBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRO_MARCASBindingNavigatorSaveItem.Click
        Try
            Validate()
            PRO_MARCASBindingSource.EndEdit()
            daMarcas.Update(DsProductos.PRO_MARCAS)
            MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fMarcas_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Me.DsProductos.PRO_MARCAS.GetChanges() IsNot Nothing Then
                PRO_MARCASBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    PRO_MARCASBindingNavigatorSaveItem.PerformClick()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fMarcas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daMarcas = New dsProductosTableAdapters.PRO_MARCASTableAdapter
            daClave = New dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub PRO_MARCASDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles PRO_MARCASDataGridView.CellBeginEdit
        If e.ColumnIndex = 1 AndAlso PRO_MARCASDataGridView.Rows(e.RowIndex).Cells(0).Value Is DBNull.Value Then
            Dim dt As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASDataTable
            dt = daClave.GetDataByIdClavePrimaria("PRO_MARCAS")
            If dt.Count Then
                Dim dr As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASRow
                dr = dt.Rows(0)
                Dim Cons As lbGlobalcom.ConsecTablas = New lbGlobalcom.ConsecTablas(dr("TABLA"), dr("CAMPO"), dr("LONGITUD"))
                PRO_MARCASDataGridView.Rows(e.RowIndex).Cells(0).Value = Cons.NumeroConsec
                Cons = Nothing
                PRO_MARCASBindingSource.EndEdit()
                daMarcas.Update(DsProductos.PRO_MARCAS)
            End If
        End If
    End Sub

    Private Sub PRO_MARCASDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles PRO_MARCASDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub


    Private Sub PRO_MARCASDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles PRO_MARCASDataGridView.UserDeletedRow
        Try
            daMarcas.Update(DsProductos.PRO_MARCAS)
            MessageBox.Show("Registro borrado con exito", "Elimina registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub PRO_MARCASDataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles PRO_MARCASDataGridView.UserDeletingRow
        If MessageBox.Show("Desea borrar el registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("Desea borrar el registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            PRO_MARCASBindingSource.Remove(PRO_MARCASBindingSource.Current)
            PRO_MARCASBindingNavigatorSaveItem.PerformClick()
           End If
    End Sub


End Class